package goodbaby.dklt.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.tgd.gbb.uikit.ui.widget.asyncimageview.CircleImageView;

import java.lang.reflect.Type;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.adapter.CommonAdapter;
import goodbaby.dklt.adapter.ViewHolder;
import goodbaby.dklt.bean.ClassListInfo;
import goodbaby.dklt.fragment.ClassFragment;
import goodbaby.dklt.fragment.ManagerFragment;

/**
 * Created by Jayden on 2016/5/10 16:27.
 *
 * @version V1.0
 * @Title:
 * @Description: ${todo}
 */
public class ClassActivity extends BaseActivity {

    private TextView tvTitle;
    private ImageView ivBack;

    private GridView gridView;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_class;
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle = (TextView) findViewById(R.id.tv_common_title);
        tvTitle.setText("班级");
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        gridView = (GridView) findViewById(R.id.gv_gridview);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String classId = ((ClassGridAdapter) gridView.getAdapter()).getItem(position).getID() + "";
                Intent intent = new Intent(ClassActivity.this, CourseActivity.class);
                intent.putExtra("fromItem", ManagerFragment.fromCourse);
                intent.putExtra("classId", classId);
                startActivity(intent);
            }

        });

    }

    @Override
    public void initData() {
        super.initData();
        ActionHelp.getClassList(activity, new ObjectCallback<List<ClassListInfo>>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(ClassActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                ToastUtil.showToast(activity, "失败");
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "失败");
            }

            public void onSuccess(List<ClassListInfo> paramAnonymousList) {
                gridView.setAdapter(new ClassGridAdapter(activity, paramAnonymousList));
            }

            @Override
            public Type getType() {
                return new TypeToken<List<ClassListInfo>>() {
                }.getType();
            }
        });
    }

    private class ClassGridAdapter extends CommonAdapter<ClassListInfo> {
        private int[] classImages = new int[]{
                R.mipmap.class_1, R.mipmap.class_2, R.mipmap.class_3,
                R.mipmap.class_4, R.mipmap.class_5, R.mipmap.class_6,
                R.mipmap.class_7, R.mipmap.class_8, R.mipmap.class_9};

        public ClassGridAdapter(Context context, List<ClassListInfo> mDatas) {
            super(context, R.layout.item_grid_calss_student, mDatas);
        }

        @Override
        public void convert(ViewHolder helper, ClassListInfo item, int position) {
            helper.setText(R.id.tv_username, item.getNAME());

            CircleImageView image = helper.getView(R.id.civ_head_image);
            image.setImageResource(classImages[position % 9]);

        }
    }


    @Override
    public void viewClick(View v) {
        super.viewClick(v);
        switch (v.getId()) {
            case R.id.iv_common_back:
                finish();
                break;
        }
    }
}
